import {base_url} from "../../config/config.js"
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    loading: true,
    myjobs:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let stuid = app.globalData.user_id;
    let that = this;
    wx.request({
      url: base_url + "/stu/work?stuid=" + stuid,
      method: "GET",
      success(res) {
        console.log(res)
        if (res.statusCode == 200){
          that.setData({
            myjobs: res.data.data
          });
        } else {
          wx.showToast({
            title: '获取数据失败！',
            icon: "none"
          })
        }
      },
      fail(res) {
        wx.showToast({
          title: '获取数据失败！',
          icon: "none"
        })
      }
    })
  },
  goto_job_detail(e) {
    let job_id = e.currentTarget.dataset.jobid
    let merchantid = e.currentTarget.dataset.merchantid
    wx.navigateTo({
      url: '../job_detail/job_detail?job_id=' + job_id + "&merchantid=" + merchantid,
    })
  },

  cancel_job(e){
    let that = this;
    console.log(e);
    let job_id = e.currentTarget.dataset.jobid;
    let stuid = app.globalData.user_id;
    
    wx.request({
      url: base_url + "/stu/work",
      method: "DELETE",
      data:{
        "detailid": job_id,
        "stuid": stuid
      },
      success(res) {
        console.log(res)
        if (res.statusCode == 200) {
          let jobs = [];
          for (var i = 0; i < that.data.myjobs.length; i++){
            let id = that.data.myjobs[i].id;
            console.log(id + " " + job_id)
            if (that.data.myjobs[i].id != job_id){
              jobs.push(that.data.myjobs[i])
            }
          }
          that.setData({
            "myjobs": jobs
          });
          wx.showToast({
            title: '操作成功！',
            icon: "none"
          })
        } else {
          wx.showToast({
            title: '操作失败！',
            icon: "none"
          })
        }
      },
      fail(res) {
        wx.showToast({
          title: '操作失败！',
          icon: "none"
        })
      }
    })
  },
 
  onReady() {
    this.setData({
      loading: false
    });
  }
})